package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingService shoppingService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        lqw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //添加到购物车的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            lqw.eq(ShoppingCart::getSetmealId, setmealId);
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingService.getOne(lqw);

        if (cartServiceOne != null) {
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingService.updateById(cartServiceOne);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingService.list(lqw);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingService.remove(lqw);
        return R.success("清空购物车成功");
    }

//    @PostMapping("/sub")
//    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
//        Long userId = BaseContext.getCurrentId();
//        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(ShoppingCart::getUserId,userId);
//        if (shoppingCart.getDishId()!=null){
//            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
//        }else{
//            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//        }
//        ShoppingCart shoppingCart1 = shoppingService.getOne(lqw);
//
//            Integer number = shoppingCart1.getNumber();
//
//            if (number == 1){
//                shoppingService.removeById(shoppingCart1);
//            }else {
//                shoppingCart1.setNumber(number-1);
//                shoppingService.updateById(shoppingCart1);
//            }
//
//        return R.success("删除成功");
//    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        if (shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingServiceOne = shoppingService.getOne(lqw);

        Integer number = shoppingServiceOne.getNumber();


        if (number == 1) {
            shoppingService.removeById(shoppingServiceOne);
        } else {
            shoppingServiceOne.setNumber(number - 1);
            shoppingService.updateById(shoppingServiceOne);
        }

        return R.success("删除成功！");
    }
}
